//
//  UIBarButtonItem+MTExtension.m
//  maTuanWang
//
//  Created by 帮友贷 on 16/2/24.
//  Copyright © 2016年 帮友贷. All rights reserved.
//

#import "UIBarButtonItem+MTExtension.h"
#import "UIView+MTExtension.h"

@implementation UIBarButtonItem (MTExtension)

+ (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    button.size = button.currentBackgroundImage.size;
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[self alloc] initWithCustomView:button];
}

+ (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage titleString:(NSString *)title titleColor:(UIColor *)titleColor leftOrRight:(BOOL)left target:(id)target action:(SEL)action{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [leftButton setTitle:title forState:UIControlStateNormal];
    [leftButton setTitleColor:titleColor forState:UIControlStateNormal];
//    [leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
    [leftButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [leftButton setImage:[[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [leftButton setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    leftButton.size = CGSizeMake(50 *SCREEN_SCALE , 30);
     leftButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    leftButton.imageView.contentMode = UIViewContentModeRight;
    
    // 让按钮内部的所有内容左对齐
    
    if (left) {
        leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //        [button sizeToFit];
        // 让按钮的内容往左边偏移10
        leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    } else {
        leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        // 让按钮的内容往右边偏移10    UIEdgeInsetsMake(0, 0, 0, 0)
        leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
        CGFloat imageWidth = leftButton.imageView.bounds.size.width;
        CGFloat labelWidth = leftButton.titleLabel.bounds.size.width;
        
        leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
        leftButton.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);

    }
    
    [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    // 修改导航栏左边的item
    // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    return (UIBarButtonItem *) [[self alloc] initWithCustomView:leftButton];
}

@end
